<template>
	<view style="position:fixed;top:0;bottom:0;left:0;right:0;scroll:hidden;">
		<view class="content" :style="{minHeight:$height(20)}">
			<view class="menu">
				<u-button :custom-style="resetBtnStyle" shape="circle" @click="resetHandle">重写</u-button>
				<u-button :custom-style="confirmBtnStyle" shape="circle" @click="submitHandle">确定</u-button>
			</view>
			<view class="canvas">
				<!-- #ifdef MP-WEIXIN -->
				<Signature ref="signature" />
				<!-- #endif -->
				<!-- #ifdef MP-TOUTIAO -->
				<Signature ref="signature" />
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	import Signature from './canvas.vue'
	export default {
		components: {
			Signature
		},
		data() {
			return {
				canvasSize: {
					width: 0,
					height: 0
				},
				resetBtnStyle: {
					width: '174rpx',
					height: '80rpx',
					transform: 'rotate(90deg)',
					'margin-top': '130rpx',
					color: '#2E6ADF',
					'font-weight': '500'
				},
				confirmBtnStyle: {
					width: '174rpx',
					height: '80rpx',
					transform: 'rotate(90deg)',
					'margin-bottom': '130rpx',
					background: '#2E6ADF',
					color: '#ffffff',
					'font-weight': '500'
				}
			}
		},
		
		methods: {
			// 处理签名重写
			resetHandle() {
				uni.showModal({
					title: '是否重新签名',
					success: (res) => {
						this.$refs.signature.$clear()
					}
				})
			},
			// 处理提交签名
			async submitHandle() {
				try {
					const tmpPath = await this.$refs.signature.$createImage()
					// 上传签名到服务器
					uni.uploadFile({
						url: this.$axios.interfaceUrl() + "/upload", //仅为示例，非真实的接口地址
						filePath: tmpPath,
						formData: {
							isSign: 1
						},
						name: 'file',
						success: (res) => {

							var res = JSON.parse(res.data)
							
							uni.$emit('doSign', res.data.src)
							this.$nextTick(() => {
								uni.navigateBack()
							})
				
						}
					})
				} catch (err) {
					// 签名保存失败,请重试
					console.log(err)
				}
			}
		}

	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		// width: 710rpx;
		height: 80vh;
		margin: 5vh auto 0;

		.menu {
			display: flex;
			flex-flow: column;
			align-items: center;
			justify-content: space-between;
			width: 164rpx;
			height: 100%;
		}

		.canvas {
			width: 526rpx;
			height: 90%;
			box-shadow: 2rpx 4rpx 20rpx rgba(4, 35, 41, 0.19);
			position: relative;
		}
	}

	#canvas {
		width: 100vw;
		height: 100vh;
	}
</style>